package com.gitee.wsl.android.task

import androidx.lifecycle.LifecycleOwner
import androidx.work.WorkManager

import com.gitee.wsl.android.ext.ApplicationInit.application

import com.gitee.wsl.ext.base.isTrue
import com.gitee.wsl.ext.coroutines.doOnLifecycle

import com.gitee.wsl.service.task.SampleTask
import com.gitee.wsl.service.task.scope.JobTaskScope
import com.gitee.wsl.service.SampleTaskManager

import java.util.UUID


val workManager: WorkManager
    get() = WorkManager.getInstance(application)

//fun TaskManager.addJob(workerId:UUID, lifecycle: LifecycleOwner?){
//    TaskManager.sampleTaskManager.addJob(workerId, lifecycle)
//}

fun SampleTaskManager.addJob(workerId: UUID, lifecycle: LifecycleOwner?){
    val worker = WorkerTask(workerId)
    addNewTask(worker)
    lifecycle?.doOnLifecycle {
        worker.cancel()
        removeTask(worker)
    }
}

class WorkerTask(val workerId: UUID): SampleTask<JobTaskScope>(),JobTaskScope {

    override fun isActive(): Boolean {
        return !workManager.getWorkInfoById(workerId).get().state.isFinished
    }

    override fun pause() {
    }

    override fun cancel() {
        stop()
    }

    override fun start() {
    }

    override fun stop() {
        isActive().isTrue {
            workManager.cancelWorkById(workerId)
        }
    }
}


/*class CronTimeTaskServer: AbstractTaskService<CronTimeTaskImp, CornTimeTaskScope, TaskStatus>() {
    companion object {
        private val cronTimerTaskManager by lazy { CronTimeTaskServer() }

        fun cronRun(id:String,cronString: String,flag: Long=0):CronTimeTaskImp{
            return cronTimerTaskManager.createNewTask(id,cronString,flag)
        }
    }

    override fun initServer() {
    }

    fun createNewTask(id:String,cronString: String,flag: Long=0):CronTimeTaskImp{
        val task=CronTimeTaskImp(Alarm.alarmServer,CronTaskItem(id,cronString,flag=flag))
        task.executionTime()
        addNewTask(task)
        return task
    }
}*/




/*
class CronTimeTaskImp(val alarmServer: AlarmServer, private val cronTaskItem: CronTaskItem):
    AbstractRetryAbleTask<CornTimeTaskScope, TaskStatus>(if(cronTaskItem.able) TaskStatus.UNKNOWN else TaskStatus.STOPPED ),
    CornTimeTask {

    override fun getTaskTag(): String {
        return cronTaskItem.id
    }

    override fun start() {}

    override fun stop() {}
    override fun retry(): Int {
        return 0
    }


    override fun pause() {
        cronTaskItem.able = false
    }

    override fun cancel() {    }

    fun executionTime(){
        val cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
        val parser = CronParser(cronDefinition)
        val executionTime = ExecutionTime.forCron(parser.parse(cronTaskItem.cronString))
        val netAlterTime=executionTime.nextExecution(currentZoneDateTime)
        cronTaskItem.hasNext = netAlterTime.isPresent

        */
/*val builder = KCron.parseAndBuild(cronTaskItem.cronString, WeekDays.Sunday)
        builder.nextRun?.let {
            cronTaskItem.hasNext =true
        }*//*

    }

}
*/